package day3;
import java.io.*;

//测试序列化
public class TestSerializable {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //output();
        input();
    }
    //反序列化
    public static void input() throws IOException, ClassNotFoundException {
        FileInputStream fis=new FileInputStream("d://io//student.xxx");
        ObjectInputStream ois=new ObjectInputStream(fis);
        Student stu=(Student)ois.readObject();
        System.out.println(stu.getName()+" "+stu.getId()+" "+stu.getScore());
        System.out.println(stu.getInfo().getPhone());
    }
    //序列化
    public static void output() throws IOException {
        Student s=new Student();
        s.setName("零零七");
        s.setId("007");
        s.setScore(100);
        StudentIfo ifo=new StudentIfo();
        ifo.setId("007");
        ifo.setPhone(110);
        s.setInfo(ifo);
        //因为序列化时转换为字节序列 所以文件内容不能修改
        //所以本地文件会设置一个无法打开的后缀名
        FileOutputStream fos=new FileOutputStream("d://io//student.xxx");
        ObjectOutputStream oos=new ObjectOutputStream(fos);
        oos.writeObject(s);
    }
}
//序列化必须要实现该接口 否则报错
class Student implements Serializable {
    //序列化还有默认的唯一标识:作用用于检测序列化和反序列化是否为同一个ID
    private static final long serialVersionUID=1111111111111111111l;
    private String id;
    //忽略序列化 属性的值不保存 static transient
    private static String name;
    private transient double score;
    private StudentIfo info;

    public StudentIfo getInfo() {
        return info;
    }
    public void setInfo(StudentIfo info) {
        this.info = info;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }
}
class StudentIfo implements Serializable{
    private String id;
    private String sex;
    private int phone;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getPhone() {
        return phone;
    }

    public void setPhone(int phone) {
        this.phone = phone;
    }
}

